<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\DiyFormContent;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;

class DiyFormContentController extends AdminController
{
    public function index(Content $content)
    {
        $slug = request()->input('mini_type');

        $tab = Tab::make();
        $tab->addLink('自定义表单', admin_route('diy_form.index', ['mini_type' => $slug]));
        $tab->add('表单提交内容', $this->grid(), true);

        return $content->body($tab->withCard());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DiyFormContent(['member']), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map))->orderByDesc('id');

            $grid->column('id');
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('diy_form_id')->link(function ($id) {
                return admin_route('diy_form.index', ['id' => $id]);
            }, '_self');
            $grid->column('diy_form_name');
            $grid->column('member_id');
            $grid->column('member.nickname', '会员名称');
            $grid->column('member.phone', '会员手机号');
            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) use ($map) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->equal('diy_form_id')->width(2);
                $filter->like('diy_form_name')->width(2);
                $filter->equal('member_id')->width(2);
                $filter->between('created_at')->datetime()->width(3);
            });

            $grid->disableBatchDelete();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();
        });
    }

    protected function detail($id)
    {
        $info = \App\Models\DiyFormContent::find($id);

        $info->content = json_decode($info->content, true);

        return admin_view('admin.diy_form.show_form_content', ['info' => $info]);
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DiyFormContent(), function (Form $form) {
            $form->display('id');
            $form->text('diy_form_id');
            $form->text('member_id');
            $form->text('mini_type');
            $form->text('content');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
